package com.wl156.activities.order;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyOrderDetail extends Activity {
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;
	private String orderID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		orderID = getIntent().getStringExtra("id");

		setContentView(R.layout.act_order_detail);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		new AsyncTask<Void, Void, Object>() {
			protected void onPostExecute(Object result) {
				if (result instanceof Exception) {
					Exception e = (Exception) result;
					Toast.makeText(MyOrderDetail.this, "加载失败，请稍后再试",
							Toast.LENGTH_SHORT).show();
				} else {
					SoapObject so = (SoapObject) result;

					LinearLayout ll = (LinearLayout) findViewById(R.id.root);
					for (int i = 0; i < ll.getChildCount(); i++) {
						LinearLayout child = (LinearLayout) ll.getChildAt(i);
						TextView tv = (TextView) child.getChildAt(1);
						tv.setText(Utils.propertyToShow(so.getProperty(i + 1)));
					}
				}
			}

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.company_getMyOrderView(
							mCertificate, orderID);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();

	}
}
